<?php

namespace APP\Http\Controllers\Admin;

use App\Type;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TypeController extends Controller
{
    public function index(Request $request)
    {
        $types = Type::orderBy('fullpath', 'asc')->get();
        return view('admin.type.index', [
            'request' => $request,
            'types' => $types
        ]);
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'required'
            ];
            $messages = [
                'name.required' => '请输入车厂名！'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $type = Type::find($data['id']);
            $type->name = $data['name'];
            $type->save();
            return redirect('admin/type');
        }
        $type = Type::find($id);
        if (!isset($type->name)) {
            return redirect('admin/type');
        }
        $count = count(explode(',', $type->fullpath));
        if ($count == 1) {
            $str = '车厂';
        } elseif ($count == 2) {
            $str = '车型';
        } elseif ($count == 3) {
            $str = '产品';
        }
        return view('admin.type.edit', [
            'request' => $request,
            'type' => $type,
            'str' => $str
        ]);
    }

    public function factory(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'required|min:2|max:20',
            ];
            $messages = [
                'name.required' => '请输入车厂名！',
                'name.min' => '车厂名至少2个字符！',
                'name.max' => '车厂名最多20个字符！',
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $data['parent_id'] = 0;
            $result = Type::create($data);
            $result->fullpath = $result->id;
            $result->save();
            return redirect('admin/type');
        }
        return view('admin.type.factory', [
            'request' => $request
        ]);
    }

    public function model(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'factory_id' => 'required|integer',
                'name' => 'required|min:2|max:20',
            ];
            $messages = [
                'factory_id.required' => '请选择车厂',
                'factory_id.integer' => '请选择车厂',
                'name.required' => '请输入车型名！',
                'name.min' => '车型名至少2个字符！',
                'name.max' => '车型名最多20个字符！',
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $result = Type::create([
                'parent_id' => $data['factory_id'],
                'name' => $data['name']
            ]);
            $result->fullpath = $data['factory_id'] . ',' . $result->id;
            $result->save();
            return redirect('admin/type');
        }
        $factories = Type::where('parent_id', '=', '0')->get();
        return view('admin.type.model', [
            'request' => $request,
            'factories' => $factories
        ]);
    }

    public function product(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'factory_id' => 'required|integer',
                'model_id' => 'required|integer',
                'name' => 'required|min:2|max:20',
            ];
            $messages = [
                'factory_id.required' => '请选择车厂',
                'factory_id.integer' => '请选择车厂',
                'model_id.required' => '请选择车型',
                'model_id.integer' => '请选择车型',
                'name.required' => '请输入产品名！',
                'name.min' => '产品名至少2个字符！',
                'name.max' => '产品名最多20个字符！',
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $result = Type::create([
                'parent_id' => $data['model_id'],
                'name' => $data['name']
            ]);
            $result->fullpath = $data['factory_id'] . ',' . $data['model_id'] . ',' . $result->id;
            $result->save();
            return redirect('admin/type');
        }
        $factories = Type::where('parent_id', '=', '0')->get();
        return view('admin.type.product', [
            'request' => $request,
            'factories' => $factories
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $type = Type::find($id);
        if ($type->delete()) {
            $return = ['code' => 0];
        } else {
            $return = ['code' => __LINE__, 'msg' => '删除失败'];
        }
        return response()->json($return);
    }

    public function getModel(Request $request)
    {
        $id = $request->input('id');
        $html = '<option value="">--请选择--</option>';
        if (!empty($id)) {
            $models = Type::where('parent_id', '=', $id)->get();
            foreach ($models as $model) {
                $html .= '<option value="' . $model->id . '">' . $model->name . '</option>';
            }
        }
        $return = ['code' => 0, 'msg' => $html];
        return response()->json($return);
    }
}